import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"
For this excercise, we have written the following code to load the stock dataset built into plotly express.
stocks = px.data.stocks()
stocks.head()
Select a stock and create a suitable plot for it. Make sure the plot is readable with relevant information, such as date, values.
import numpy as np
fig, ax = plt.subplots(figsize=(12,9))
x = stocks['date']
y = stocks['GOOG']
plt.plot(x,y)
ax.set_xticks(np.arange(0, len(x)+1, 14)) #Length of file = 105, steps of 14 makes 8 dates show in the x-axis.
ax.set_xlabel('date')
ax.set_ylabel('stock value')
ax.set_title('Google stock');
You've already plot data from one stock. It is possible to plot multiples of them to support comparison.
To highlight different lines, customise line styles, markers, colors and include a legend to the plot.
stocks.plot(x = 'date', figsize=(12,9), xticks=np.arange(0, len(x)+1, 14), xlabel = 'date', ylabel = 'stock value', title = 'Stocks');
First, load the tips dataset
tips = sns.load_dataset('tips')
tips.head()
Let's explore this dataset. Pose a question and create a plot that support drawing answers for your question.
Some possible questions:
print("Question: Are there differences between male and female when it comes to giving tips?")
sns.scatterplot(x='total_bill', y='tip', data=tips, hue = 'sex')
plt.show()
print("Answer: The scatterplot shows that give a little more tip than women.")
print("The average tip men give is",
round(tips[tips.sex == 'Male'].tip.mean(),2), ", while women on average give a tip of",
round(tips[tips.sex == 'Female'].tip.mean(),2),'.');
Redo the above exercises (question 2 & 3) with plotly express. Create diagrams which you can interact with.
Hints:
stock_names = stocks.columns[1:]
print(stock_names)
fig1 = px.line(stocks, x = 'date', y = stock_names, markers = True)
fig1.show();
#fig2 = px.scatter(tips, x='total_bill', y='tip', color='sex')
#fig2.show()
fig1 = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="smoker", facet_row="time")
fig1.show(figsize = (12,9));
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
#load data
df = px.data.gapminder()
df.head()
df_2007 = df.query('year==2007')
df_2007_new = df_2007.groupby('continent').sum()
fig = px.bar(df_2007_new, x="pop", y=df_2007_new.index, orientation='h',
color = df_2007_new["pop"].astype(str), text='pop')
fig.update_yaxes(categoryorder="total ascending")
fig.update_traces(textposition='outside', showlegend=False,texttemplate = "%{text:.2s}")
fig.update_layout(uniformtext_minsize=12)
fig.show()